package com.cskaoyan.service.user.impl;

import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketAddress;
import com.cskaoyan.bean.po.MarketAddressExample;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.service.user.UserAddressService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jzp
 * @since 2022/11/19 21:39
 */
@Service
public class UserAddressServiceImpl implements UserAddressService {
    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Override
    public CommonData list(BasePageInfo info, String name, String userId) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name.trim() + "%");
        }
        if (!StringUtil.isEmpty(userId)) {
            try {
                criteria.andUserIdEqualTo(Integer.valueOf(userId));
            }catch (Exception e){
                return null;
            }

        }
        // 排序
        example.setOrderByClause(info.getSort()+" "+info.getOrder());

        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(example);

        PageInfo<MarketAddress> pageInfo = new PageInfo<>(marketAddresses);

        return CommonData.data(pageInfo);

    }
}
